package com.yl.config.event;

import com.google.common.eventbus.AsyncEventBus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Todo
 * @Author jiangtao
 * @CreateBy 2020-06-15 4:45 PM
 * @UpdateBy jiangtao
 * @Version v1.0
 */
@Configuration
public class AsyncEventBusConfig {

    @Bean
    @Scope("singleton")
    public AsyncEventBus asyncEventBus() {
        //创建线程池对象
        final ThreadPoolExecutor executor = myExecutor(5);
        return new AsyncEventBus(executor);
    }

    private ThreadPoolExecutor myExecutor(int nThreads) {
        // 固定线程池 corePoolSize = maximumPoolSize
        return new ThreadPoolExecutor(
                nThreads,
                nThreads,
                0L,
                TimeUnit.MICROSECONDS,
                new LinkedBlockingDeque<>());
    }
}
